home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
DB_CLIPP
/
0411.ZIP
/
FILEDUMP.C
< prev
next >
Wrap
Text File
|
1985-11-01
|
3KB
|
140 lines
/*
* Program to read a file and display a hex value and - if possible -
* ASCII char for each byte read.
*/
#include "stdio.h"
main(argc,argv)
int argc;
char *argv[];
{
char buffer[0x18],
exfname[64];
int c,
numoff,
linecntr,
buffend,
buffposn,
eofinp,
outfile,
infile;
if ((argc == 1) || (argv[1][0] == '?'))
{
puts("\n");
puts("Syntax: FILEDUMP <infile> [<outfile>] [/n]\n");
puts("where infile is the filespec for the input data\n");
puts(" outfile is the filespec for the output data\n");
puts(" /n indicates that the output is to be numbered\n");
puts("\n");
puts("The input file's characters are output in lines of 24 bytes.");
puts("\n");
puts("The hex value of the 24 bytes are listed first in blocks\n");
puts("of 4, followed by the ASCII representation of the bytes if\n");
puts("there is one. If the byte is not a printable character, it\n");
puts("is replaced in the output by a period (\".\").\n");
puts("If no output file is specified, the output is directed to\n");
puts("the screen.\n");
puts("\n");
exit(0);
}
if (argv[1][0] == '#')
{
puts("\n");
puts("PROGRAM: FILEDUMP\n");
puts("Author : Peter Townsend\n");
puts("Date : 31Oct85\n");
puts("Version: 1.3\n");
exit(0);
}
if ((infile = open(argv[1],0)) == -1)
{
printf("\nCannot open input file %s\n",argv[1]);
exit(1);
}
numoff = TRUE;
if (argc == 2)
{
strcpy(exfname,"CON");
}
else
{
c = ((strcmp(argv[2],"/n") == 0) || (strcmp(argv[2],"/N") == 0));
if (c)
{
strcpy(exfname,"CON");
numoff = FALSE;
}
else
{
strcpy(exfname,argv[2]);
}
}
if (strcmp(argv[1],exfname) == 0)
{
printf("\nInput filename and output filename cannot be the same.\n");
exit(1);
}
if ((outfile = open(exfname,1)) == -1)
{
if ((outfile = creat(exfname)) == -1)
{
printf("\nCannot create output file %s\n",exfname);
exit(1);
}
}
if ((strcmp(argv[3],"/n") == 0) || (strcmp(argv[3],"/N") == 0))
{
numoff = FALSE;
}
linecntr = 0;
eofinp = FALSE;
while (!eofinp)
{
buffend = read(infile,buffer,0x18);
eofinp = (buffend < 0x18);
buffposn = 0;
if (!numoff)
{
fprintf(outfile,"%04x-",linecntr);
linecntr = linecntr + buffend - 1;
fprintf(outfile,"%04x",linecntr);
}
while (buffposn < buffend)
{
if ((buffposn % 4) == 0)
{
fprintf(outfile,"%c",' ');
}
fprintf(outfile,"%02x",buffer[buffposn]);
buffposn++;
}
while (buffposn < 24)
{
if ((buffposn % 4) == 0)
{
fprintf(outfile,"%c",' ');
}
fprintf(outfile,"%c%c",' ',' ');
buffposn++;
}
fprintf(outfile,"%c",' ');
buffposn = 0;
while (buffposn < buffend)
{
if (isprint(buffer[buffposn]))
{
fprintf(outfile,"%c",buffer[buffposn]);
}
else
{
fprintf(outfile,"%c",'.');
}
buffposn++;
}
fprintf(outfile,"%c",'\n');
linecntr++;
}
fclose(infile);
fclose(outfile);
exit(0);
}